{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5fd54556-0bfd-4365-966f-d2a7e3c1a7a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchvision\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.utils.tensorboard import SummaryWriter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2226b7d9-0e71-4e06-b8e4-d1a6d3de4d36",
   "metadata": {},
   "source": [
    "# dataset类比为牌堆，dataloader可以类比为抽牌方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a88bc195-27b0-4f95-b03c-05cef878b896",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_transform = torchvision.transforms.Compose([\n",
    "    torchvision.transforms.Resize((100,100)),\n",
    "    torchvision.transforms.ToTensor()\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3649661-62ef-49d0-93ac-6c8b859ed75a",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_set = torchvision.datasets.CIFAR10(root='./data_set',train= True,transform=dataset_transform,download=True)\n",
    "test_set = torchvision.datasets.CIFAR10(root='./data_set',train= False,transform=dataset_transform,download=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33ec4c2e-524b-4299-88cb-edf96321016e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(test_set[0])\n",
    "# print(test_set.classes)\n",
    "# img, target = test_set[0]\n",
    "# img.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "905b9da2-389b-4902-80ab-91bdb2076ecd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "img_tensor,target = test_set[0] # test_set[0]相当于重定向 test_set.getitem(0)\n",
    "print(test_set[0][1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2821b25d-85e1-49a1-bc5a-0ab3f78ce7dc",
   "metadata": {},
   "source": [
    "**DataLoader 使用方法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "55bfd116-14fa-4a3f-9623-e3b213d683c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_dataloader = DataLoader(dataset= test_set,batch_size= 4,shuffle= True,num_workers=0,drop_last= False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d846f97b-1ac2-46fb-9b6e-16ebeab4dc5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "writer = SummaryWriter(\"dataloader\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c2c5e0c3-7822-4fc8-9ac0-bc420a16668f",
   "metadata": {},
   "outputs": [],
   "source": [
    "step = 0\n",
    "for data in test_dataloader:\n",
    "    imgs,targets = data\n",
    "    writer.add_images(\"testdata\",imgs,step)\n",
    "    step += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b6419c08-d147-42f3-88e6-43e866cbf387",
   "metadata": {},
   "outputs": [],
   "source": [
    "writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7da08171-89d3-4bb4-9fb4-88f30d544dc4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
